package com.qualcomm.vuforia.CloudRecognition.implement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import java.util.Enumeration;

import javax.servlet.http.Cookie;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

public class FakeHttpServletRequest extends HttpServletRequestWrapper {

	public FakeHttpServletRequest(HttpServletRequest request) {
		super(request);
	}
	
	public String getHeader(String name) {
		HttpServletRequest request = (HttpServletRequest) getRequest();
		
		if (name.equals("username")) {
			Cookie[] cookies = request.getCookies();
			
			if (cookies == null) return null;
			
			for (Cookie aCookie : cookies) {
				if (aCookie.getName().equals("username")) {
					return aCookie.getValue();
				}
			}
		}
		
		return request.getHeader(name);
	}
	
	public Enumeration<String> getHeaderNames() {
		HttpServletRequest request = (HttpServletRequest) getRequest();
		Enumeration<String> headers = request.getHeaderNames();
		
		List<String> listHeaders = new ArrayList<>();
		
		while (headers.hasMoreElements()) listHeaders.add(headers.nextElement());
		
		listHeaders.add("username");
		
		return Collections.enumeration(listHeaders);
	}
	
}
